var parentData, parentActive;
var dateFormatStr = 'yyyy-MM-dd HH:mm:ss';
layui.use(['form', 'layer', 'table'], function () {
	var form = layui.form,
		table = layui.table,
		$ = layui.$;

	form.verify({});
	
	var formdata;

	window.active = {
		select: (data) => {
			formdata = form.val("dataform");
			//调整结构
			formdata.game_id = formdata.id;
			formdata.goods_id = data.id;
			delete formdata.id;
		}
	};

	//表格按钮操作
	table.on('tool(tableList)', function (obj) {
		var type = obj.event;
		active[type] ? active[type].call(this, obj.data) : '';
	});

	//延时初始化，确保数据已经塞进来了
	setTimeout(() => {
		if (!parentData) return;
		//初始化数据
		form.val("dataform", JSON.parse(JSON.stringify(parentData)));
	}, 200);
	
	form.on("submit(save)", element => {
		popSubmit('gameLottery/prize', formdata);
		return false;
	});

	$('.pic-cont').click(function () {
		document.getElementById('J-update-img-file').click();
	});
	$('#J-update-img-file').on('change', async function (e) {
		if (!this.files[0] || !window.FileReader) return;
		if (this.files[0].type !== 'image/jpeg') {
			alert('请选择jpg格式的图片!');
			return;
		}

		imgArtwork = await ci.compressImg(this.files[0], {
			size: 0.5,
			Rectangle: true
		});
		imgArtwork.path = URL.createObjectURL(imgArtwork);
		imgArtwork.name = this.files[0].name;
		imgUrl = imgArtwork.path;
		$(".pic-cont").css('background-size', 'cover');
		$(".pic-cont").css('background-image', `url(${imgUrl})`);
		return;
	});
});